package com.ruoyi.report.controller;

import java.util.List;

import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.domain.XlkjReport;
import com.ruoyi.report.service.IXlkjReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 举报投诉Controller
 *
 * @author wang
 * @date 2022-01-15
 */
@RestController
@RequestMapping("/report/report")
public class XlkjReportController extends BaseController
{
    @Autowired
    private IXlkjReportService xlkjReportService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询举报投诉列表
     */
    @PreAuthorize("@ss.hasPermi('report:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjReport xlkjReport)
    {
        startPage();
        List<XlkjReport> list = xlkjReportService.selectXlkjReportList(xlkjReport);
        for(XlkjReport report:list){
            //多图片路径处理
            if(report.getImg() != null){
                String[] split = report.getImg().split(",");
                if(split.length > 1) {
                    String imgStr = "";
                    for (int i = 0; i < split.length; i++) {
                        imgStr += UrlUtils.getUrl() + split[i];
                        if (i != split.length - 1) {
                            imgStr += ",";
                        }
                    }
                    report.setImg(imgStr);
                }else {
                    report.setImg(UrlUtils.getUrl() + report.getImg());
                }
            }
            //附件路径拼接
            if(report.getVideo() != null && !report.getVideo().equals("")){
                report.setVideo(UrlUtils.getUrl() +report.getVideo());
            }
            report.setUserName(sysUserService.selectUserById(report.getUserId()).getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出举报投诉列表
     */
    @PreAuthorize("@ss.hasPermi('report:report:export')")
    @Log(title = "举报投诉", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjReport xlkjReport)
    {
        List<XlkjReport> list = xlkjReportService.selectXlkjReportList(xlkjReport);
        ExcelUtil<XlkjReport> util = new ExcelUtil<XlkjReport>(XlkjReport.class);
        return util.exportExcel(list, "举报投诉数据");
    }

    /**
     * 获取举报投诉详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjReportService.selectXlkjReportById(id));
    }

    /**
     * 新增举报投诉
     */
    @PreAuthorize("@ss.hasPermi('report:report:add')")
    @Log(title = "举报投诉", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjReport xlkjReport)
    {
        return toAjax(xlkjReportService.insertXlkjReport(xlkjReport));
    }

    /**
     * 修改举报投诉
     */
    @PreAuthorize("@ss.hasPermi('report:report:edit')")
    @Log(title = "举报投诉", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjReport xlkjReport)
    {
        return toAjax(xlkjReportService.updateXlkjReport(xlkjReport));
    }

    /**
     * 删除举报投诉
     */
    @PreAuthorize("@ss.hasPermi('report:report:remove')")
    @Log(title = "举报投诉", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjReportService.deleteXlkjReportByIds(ids));
    }
}
